package com.yakami.ocean.view.activity;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;

import com.annimon.stream.Stream;
import com.yakami.ocean.R;
import com.yakami.ocean.ServerAPI;
import com.yakami.ocean.adapter.PagerAdapter;
import com.yakami.ocean.bean.Classify;
import com.yakami.ocean.bean.News;
import com.yakami.ocean.utils.IntentHelper;
import com.yakami.ocean.view.activity.base.BaseHoldBackTabsActivity;
import com.yakami.ocean.view.fragment.LazyNewsListFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * @author Yakami, Created on 2016/4/10
 */
public class ClassifyActivity extends BaseHoldBackTabsActivity {

    @Bind(R.id.tab_layout) TabLayout mTabLayout;
    @Bind(R.id.view_pager) ViewPager mViewPager;

    public static final String TAG = "ClassifyActivity";
    private Classify mClassify;
    private List<LazyNewsListFragment> mTabs;
    private PagerAdapter mPagerAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mClassify = (Classify) getIntentHelper().getSerializable("data");
        mTitle.setText(mClassify.name);

        initTabs(savedInstanceState != null);
    }

    @Override
    public void initTabs(boolean isRestarted) {
        super.initTabs(isRestarted);
        mPagerAdapter = new PagerAdapter(getSupportFragmentManager());
        if (mPagerAdapter.fragmentList.size() == 0) {
            mTabs = new ArrayList<>();
            Stream.of(mClassify.child)
                    .forEach((child) -> {
                        final LazyNewsListFragment fragment = new LazyNewsListFragment();
                        fragment.setRefreshListener(() -> {
                            fragment.onLoading();
                            ServerAPI.getOceanAPI()
                                    .getClassifyPostList(mClassify.name, child)
                                    .subscribeOn(Schedulers.newThread())
                                    .observeOn(AndroidSchedulers.mainThread())
                                    .subscribe(container -> {
                                        fragment.setData(container.classifyList);
                                        fragment.onSuccess();
                                    }, throwable -> {
                                        throwable.printStackTrace();
                                        fragment.onError();
                                    });
                        });
                        fragment.setOnItemClickListener((pos, data) ->
                                startActivity(IntentHelper.newInstance(mContext, DetailActivity.class)
                                        .putSerializable("data", (News) data)
                                        .toIntent())
                        );
                        mPagerAdapter.addFragment(fragment, child);
                    });
        }
        mViewPager.setOffscreenPageLimit(mClassify.child.size());  //设置viewPager缓存页面数量
        mPagerAdapter.notifyDataSetChanged();
        mViewPager.setAdapter(mPagerAdapter);
        mTabLayout.setupWithViewPager(mViewPager);
        mPagerAdapter.notifyDataSetChanged();
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageSelected(int position) {
                mViewPager.post(() -> ((LazyNewsListFragment) mPagerAdapter.fragmentList.get(position)).requestData());
            }

            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                if (position == 0) {
                    mViewPager.post(() -> ((LazyNewsListFragment) mPagerAdapter.fragmentList.get(0)).requestData());
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }

    @Override
    protected int onBindLayout() {
        return R.layout.activity_classify;
    }


}
